<?php
App::uses('AppModel', 'Model');
/**
 * Order Model
 *
 * @property Suppliers $Suppliers
 */
class Order extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'suppliers_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'status' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'Order Quantity' => array(
            'qty-1' => array(
                'rule' => array('numeric'),
                'message' => 'Numbers only please',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => true, // Stop validation after this rule
            	//'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'qty-2' => array(
                'rule' => array('range', -1, 1000),
                'message' => 'Please enter a number between 0 and 1000',
            ),
        ),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Suppliers' => array(
			'className' => 'Suppliers',
			'foreignKey' => 'suppliers_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Delivery' => array(
			'className' => 'Deliveries',
			'foreignKey' => 'orders_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Product' => array(
			'className' => 'Products',
			'joinTable' => 'Ordersproducts',
			'foreignKey' => 'orders_id',
			'associationForeignKey' => 'products_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);

	function afterFind($results, $primary=false) 
	{ 
		foreach ($results as $key => $val) 
		{ 
			if (isset($val['Order']['created'])) 
			{ 
				$results[$key]['Order']['created'] = $this->dateFormatAfterFind($val['Order']['created']); 
			} 
		} 
		return $results; 
	} 

	function dateFormatAfterFind($dateString) 
	{ 
		return date('d/m/Y', strtotime($dateString)); 
	} 
}